/*
 * Copyright (C), 2014-2015, 联创车盟汽车服务有限公司
 * FileName: UserController.java
 * Author:   Administrator
 * Date:     2015年2月2日 上午9:39:23
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>    <time>    <version>    <desc>
 * 修改人姓名        修改时间        版本号        描述
 */
package com.linkage.controller.consumer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linkage.interceptor.NeedAdminLogin;
import com.linkage.interceptor.NeedLogin;
import com.linkage.pojo.commodity.Book;
import com.linkage.pojo.consumer.ContactDetail;
import com.linkage.pojo.consumer.User;
import com.linkage.service.commodity.BookService;
import com.linkage.service.consumer.EditContactDetailsService;
import com.linkage.service.consumer.ListPurchaseHistoryService;
import com.linkage.service.consumer.UserService;
import com.linkage.service.order.SingleOrderService;

/**
 * 〈一句话功能简述〉<br> 
 * 〈功能详细描述〉
 *
 * @author 李东峻
 *
 */
@Controller
@RequestMapping("/profile")
public class ProfileController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private EditContactDetailsService editContactDetailsService;
    
    @Autowired
    private ListPurchaseHistoryService listPurchaseHistoryService;
    
    @Autowired
    private SingleOrderService singleOrderService;
    
    @Autowired
    private BookService bookService;
    @NeedLogin
    @RequestMapping(value="/{userId}", method=RequestMethod.GET)
    public String showindex(Model model, @PathVariable("userId") Integer userId){
        model.addAttribute("userId1",userId);
        User user=this.userService.getOneUserById(userId);
        model.addAttribute("user", user);
        return "consumer/profile/profile";
    }
    
    
    
/*    @RequestMapping(value="/{userId}", method = RequestMethod.GET)
    public String showUserData(Model model, @PathVariable("userId") Integer userId){
        User user=this.uservice.getOneUserById(userId);
        model.addAttribute("user", user);
        
        return "consumer/profile/showUserData";
    }*/
    @NeedLogin
    @RequestMapping(value="/updateUserDataView/{userId}", method = RequestMethod.GET)
    public String updateUserDataView(Model model, @PathVariable("userId") Integer userId){
        model.addAttribute("userId1", userId);
        User user=this.userService.getOneUserById(userId);
        model.addAttribute("user", user);
        return "consumer/profile/updateUserDataView";
    }
    @NeedLogin    
    @RequestMapping(value="/update" ,method=RequestMethod.POST)
    public String update(User user){
        this.userService.editProfile(user);
        
        String url = "redirect:/profile/"+user.getUserId().toString();
        
      
        return url;
    }
    @NeedLogin
    @RequestMapping(value="/{userId}/contactlist" , method=RequestMethod.GET)
    public String  showContactDetail(Model model, @PathVariable("userId") Integer userId){
        model.addAttribute("userId1", userId);
        List<ContactDetail> contactDetails=this.editContactDetailsService.listAllContactDetail(userId);
        
        model.addAttribute("contactDetails", contactDetails);
        
        return "consumer/profile/showContactDetail";
        
    }
    @NeedLogin
    @RequestMapping(value="/updateContactDetail/{shoppingContactId}", method=RequestMethod.GET)
    public String showupdateContactDetail(Model model, @PathVariable("shoppingContactId") Integer shoppingContactId){
        
        ContactDetail contactDetail=this.editContactDetailsService.selectOneContactDetailById(shoppingContactId);
        
        model.addAttribute("contactDetail", contactDetail);
        
       
        return "consumer/profile/updateContactDetail";
        
    }
    @NeedLogin
    @RequestMapping(value="/updateContactDetail", method=RequestMethod.POST)
    public String updateContactDetail(ContactDetail contactDetail){
        editContactDetailsService.editContactDetails(contactDetail);
        
        String url="redirect:/profile/"+contactDetail.getUserId().toString()+"/contactlist";
        
        return url;
        
    }
    @NeedLogin
    @RequestMapping(value="/deleteContactDetail/{shoppingContactId}/{userId}" ,method=RequestMethod.GET)
    public String deleteContactDetail(@PathVariable("shoppingContactId") Integer shoppingContactId,@PathVariable("userId") Integer userId){
        
        this.editContactDetailsService.deleteContactDetails(shoppingContactId);
        //ContactDetail contactDetail=this.editContactDetailsService.selectOneContactDetailById(userId);
        
        String url="redirect:/profile/"+userId.toString()+"/contactlist";
        return url;
    }
    @NeedLogin
    @RequestMapping(value="/setDefaultContactDetail/{shoppingContactId}/{userId}", method=RequestMethod.GET)
    public String setdefaultContactDetail(@PathVariable("shoppingContactId") Integer shoppingContactId, @PathVariable("userId") Integer userId,Model model){
        model.addAttribute("userId1", userId);
        List<ContactDetail> contactDetails = this.editContactDetailsService.selectContactDetailByUserId(userId);
        
        for(int i=0;i<contactDetails.size();i++){
            
            if(contactDetails.get(i).getIsDefault()==true){
                contactDetails.get(i).setDefault(false);
                editContactDetailsService.editContactDetails(contactDetails.get(i));
            }
            //break;
        }
        
        
        editContactDetailsService.setdefaultContactDetails(shoppingContactId);
        
        String url="redirect:/profile/"+userId.toString()+"/contactlist";
        
        return url;
    }
    @NeedLogin
    @RequestMapping(value="/addContactDetail/{userId}", method=RequestMethod.GET)
    public String showaddContactDetail(Model model , @PathVariable("userId") Integer userId){
        ContactDetail contactDetail=new ContactDetail();
        model.addAttribute("userId1", userId);
        contactDetail.setUserId(userId);
        model.addAttribute("contactDetail", contactDetail);
      
        
        return "consumer/profile/addContactDetail";
    }
    @NeedLogin
    @RequestMapping(value="addContactDetail" , method=RequestMethod.POST)
    public String addContactDetail(ContactDetail contactDetail){
        
        this.editContactDetailsService.addContactDetails(contactDetail);
        
        String url="redirect:/profile/"+contactDetail.getUserId().toString()+"/contactlist";
        
        return url;
        
    
    }
    @NeedLogin
    @RequestMapping(value="/{userId}/purchasehistory" , method=RequestMethod.GET)
    public String listpurchasehistory(Model model, @PathVariable("userId") Integer userId){
        model.addAttribute("userId1", userId);
        List<Book> purchseHistorys=this.listPurchaseHistoryService.listPurchaseHistory(userId);
        
        model.addAttribute("purchaseHistorys", purchseHistorys);
        
        return "consumer/profile/listPurchaseHistory";
        
   }
    @NeedLogin
   @RequestMapping(value="/{userId}/order", method=RequestMethod.GET)
   public String showOrder(Model model , @PathVariable("userId") Integer userId){
       model.addAttribute("userId1", userId);
       List<Map<String,Object>> singleOrders=this.singleOrderService.getSingleOrderByUserId(userId);
       
       model.addAttribute("singleOrders", singleOrders);
       
       return "consumer/profile/showSingleOrder";
       
   }
    @NeedLogin
   @RequestMapping(value="/{singleOrderId}/singleOrderDetail", method=RequestMethod.GET)
   public String showSingleOrderDetail(Model model, @PathVariable("singleOrderId") Integer singleOrderId){
       
       Map<String,Object> singleOrder=singleOrderService.getSingleOrderById(singleOrderId);
       model.addAttribute("userId1", singleOrder.get("userId"));
       model.addAttribute("singleOrder", singleOrder);
       
       return "consumer/profile/showSingleOrderDetail";
   }
    @NeedLogin
   @RequestMapping(value="/{singleOrderId}/{userId}/cancleSingleOrder" , method=RequestMethod.GET)
   public String concleSingleOrder(@PathVariable("singleOrderId") Integer singleOrderId, @PathVariable("userId") Integer userId,Model model){
       model.addAttribute("userId1", userId);
       this.singleOrderService.setSingleOrderState(0, singleOrderId);
       
       String url="redirect:/profile/"+userId.toString()+"/order";
       
       return url;
   }
    @NeedLogin
   @RequestMapping(value = "/register/check/{loginName}", method = RequestMethod.POST, produces = "application/json")
   @ResponseBody
   public Map<String, Object> check(@PathVariable("loginName") String loginName ){
       Map<String, Object> params = new HashMap<>();
       boolean result = (this.userService.getOneUserByLoginName(loginName)== null);
      
       params.put("result", result);
       
       return params;
   }
    
   @RequestMapping(value="/register" , method= RequestMethod.GET)
   public String showRegister(){
       
       return "consumer/profile/register";
       
   }
   
   @RequestMapping(value="/register/action" , method=RequestMethod.POST )
   public String register(String userName, String password, String loginName, String email){
       
       this.userService.register(userName, password, loginName, email);
       
       return "concumer/profile/register";
       
   }

}
